#include<string>
#include<iostream>
#include<stdio.h>
#include<sstream>

#include "../Ask/Ask.h"

using namespace std;

void Ask_server(char * opstr2,int fd,char * reback){
	int len = strlen(opstr2);
	printf("%s\n",opstr2);
	printf("ASk = %p\n",reback);
	std::string opstr = "";

	for(int i=0;i<len;i++){
		opstr += opstr2[i];
	}

	stringstream ss;
	ss<<opstr;
	string q;

	int cnt=0;
	char ch[20][150];

	while(ss>>q){
		strcpy(ch[cnt++],q.c_str());
	}
	
	if(strcmp(ch[0],"register")==0){
		printf("ch[1] = %s\n",ch[1]);
		int cnt = finduser(ch[1]);
		if(cnt){
			printf("id = %d\n",cnt);
			strcpy(reback,"your username is already register");
			return;
		}

		int ret = Register(ch[1],ch[2]);
		if(ret){
			printf("register failed \n");
			strcpy(reback,"register failed");
			return;
		}
		strcpy(reback,"register succes");

	}else if(strcmp(ch[0],"login")==0){
		//check already login
		int id2 = iptoid(fd);
		if(id2>0||id2==-1){
			strcpy(reback,"you are already login user");
			return;
		}

		//check exite user
		int id = nametoid(ch[1]);
		
		if(!id){
			printf("login failed _ not found user_name\n");
			strcpy(reback,"not found username!");
			return;
		}
		
		//check password
		if(strcmp(ch[2],idtopwd(id).c_str())!=0){
			strcpy(reback,"wrong password!");
			return;
		}

		//login
		int ret = login(id,fd);
		if(ret){
			printf("login failed\n");
			strcpy(reback,"login failed!");
			return;
		}
		strcpy(reback,"login success");
	}else if(strcmp(ch[0],"logout")==0){
		int id = iptoid(fd);
		if(!id){
			printf("not found!");
			strcpy(reback,"you are offline!");
			return;
		}
		int ret = logout(id);
		if(ret){
			printf("logout failed");
			strcpy(reback,"logout failed!");
			return;
		}
		strcpy(reback,"logout success");
	}else if(strcmp(ch[0],"addfriend")==0){
		int id = iptoid(fd);
		if(!id){
			strcpy(reback,"you are offline!");
			return;
		}
		
		int id2 = nametoid(ch[1]);
		if(id2==0){
			strcpy(reback,"not found user!");
			return;
		}
		
		if(id==id2){
			strcpy(reback,"you can't add yourself!");
			return;
		}
		
		int ck = checkfriend(id,id2);
		if(ck){
			printf("ck = %d\n",ck);
			strcpy(reback,"you are already add friend!");
			return;
		}

		int ret = addfriend(id,id2);
		if(ret != 0){
			strcpy(reback,"addfriend failed!");
			return;
		}
		strcpy(reback,"addfriend success");

	}else if(strcmp(ch[0],"delfriend")==0){
		int id = iptoid(fd);
		if(!id){
			strcpy(reback,"you are offline!");
			return;
		}

		int id2 = nametoid(ch[1]);
		if(id2==0){
			strcpy(reback,"not found user!");
			return;
		}

		if(id==id2){
			strcpy(reback,"you can't delete yourself!");
			return;
		}

		int ret = checkfriend(id,id2);
		if(ret!=3){
			strcpy(reback,"you don't have that friend!");
			return;
		}

		ret = delfriend(id,id2);
		if(ret){
			strcpy(reback,"delfriend failed!");
			return;
		}

		strcpy(reback,"delfriend success");
	}else if(strcmp(ch[0],"send")==0){
		int id = iptoid(fd);
		if(!id){
			strcpy(reback,"you are offline!");
			return;
        }
		
		int id2 = nametoid(ch[1]);
        if(id2==0){
			strcpy(reback,"not found user!");
			return;
        }

        if(id==id2){
			strcpy(reback,"you can't send yourself!");
			return;
        }

        int ret = checkfriend(id,id2);
        if(ret != 3){
			strcpy(reback,"you don't have that friend!");
			return;
		}
		
		char mmes[1005] = "";
		int hd=0;
		for(int i=2;i<cnt;i++){
			int len=strlen(ch[i]);
			for(int j=0;j<len;j++){
				mmes[hd++]=ch[i][j];
			}
		}

		sendmes(id,id2,mmes);
		strcpy(reback,"send success");
	}else if(strcmp(ch[0],"show")==0){
		int id = iptoid(fd);
        if(!id){
			strcpy(reback,"you are offline!");
			return;
		}
		
		show(id,fd);
		strcpy(reback,"show successs");
	}else if(strcmp(ch[0],"read")==0){
		int id = iptoid(fd);
        if(!id){
            strcpy(reback,"you are offline!");
            return;
        }
 
	    int id2 = nametoid(ch[1]);
		if(id2==0){
			strcpy(reback,"not found user!");
			return;
        }

        if(id==id2){
            strcpy(reback,"you can't read yourself!");
            return;
        }
 
        int ret = checkfriend(id,id2);
        if(ret != 3){
	        strcpy(reback,"you don't have that friend!");
			return;
	    }
		ret = read(id,id2,fd);
		if(ret){
			strcpy(reback,"read failed!");
			return;
		}

		strcpy(reback,"read success");
	}else{
		strcpy(reback,"error !");
	}
	
}
